//
//  GLVideoPlayerHintView.swift
//  BigData
//
//  Created by quan on 2019/10/16.
//  Copyright © 2019 scglxx. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

/**
 *  视频播放可能出现的提示视图，如播放错误、流量播放
 */
class GLVideoPlayerHintView: UIView {
    
    var actionBlock: (()->Void)?
    
    private var contentView = UIView.init()
    var titleLabel = UILabel.init()
    private var subTitleLabel = UILabel.init()
    private var actionButton = UIButton.init(type: .custom)
    private var disposeBag = DisposeBag()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.basicSetup()
    }
    
    convenience init(title: String, buttonTitle: String, action: (()->Void)? = nil) {
        self.init(frame: .zero)
        self.actionBlock = action
        self.titleLabel.text = title
        self.setButtonTitle(title: buttonTitle)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        self.basicSetup()
    }
    
    private func basicSetup() {
        self.backgroundColor = .rgba(0, 0, 0, 0.9)
        titleLabel.textColor = .white
        titleLabel.textAlignment = .center
        titleLabel.font = .systemFont(ofSize: 15.0, weight: .bold)
        
        subTitleLabel.textColor = .init(red: 255, green: 255, blue: 255, alpha: 0.8)
        subTitleLabel.textAlignment = .center
        subTitleLabel.font = .systemFont(ofSize: 12.0, weight: .medium)
        
        actionButton.glAddCorners(rad: 15)
        actionButton.addGradualLayer(type: .fromLeftToRight, colors: [.hex(0x19C2FF), .hex(0x19E8FF)], layerFrame: CGRect(x: 0, y: 0, width: 140, height: 30))
        actionButton.rx.tap.subscribe(onNext: { [weak self] e in
            self?.actionBlock?()
        }).disposed(by: disposeBag)
        
        self.addSubview(contentView)
        contentView.addSubview(titleLabel)
//        contentView.addSubview(subTitleLabel)
        contentView.addSubview(actionButton)
        
        contentView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 180.0, height: 75))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        actionButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(25 - 8)
            make.size.equalTo(CGSize(width: 140, height: 30))
        }
        
    }
    
    
    func setButtonTitle(title: String) {
        actionButton.setTitle(title, for: .normal)
    }
}
